package com.baosight.risk.service.xtdc.impl;

import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.api.xtdc.XtdcDirectoryService;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.mapper.jlta.JltaTeamUserMapper;
import com.baosight.risk.mapper.xtdc.XtdcAttachMapper;
import com.baosight.risk.mapper.xtdc.XtdcDirectoryMapper;
import com.baosight.risk.service.jlta.entity.JltaTeamUser;
import com.baosight.risk.service.xtdc.entity.XtdcAttach;
import com.baosight.risk.service.xtdc.entity.XtdcDirectory;
import com.baosight.risk.service.xtdc.vo.DirVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author houji
 */
@Service
public class XtdcDirectoryServiceImpl extends BaseServiceImpl<XtdcDirectory> implements XtdcDirectoryService {
    @Autowired
    private XtdcDirectoryMapper directoryMapper;
    @Autowired
    private XtdcAttachMapper attachMapper;
    @Autowired
    private JltaTeamUserMapper teamUserMapper;

    @Override
    public Integer addDirectory(XtdcDirectory xtdcDirectory) {
        xtdcDirectory = (XtdcDirectory) BeanUtils.initBean(xtdcDirectory);
        return directoryMapper.insert(xtdcDirectory);
    }

    @Override
    public DirVO selectList(String rootDocId) {
        DirVO dirVO = new DirVO();
        //查找出根目录
        XtdcDirectory directory = directoryMapper.selectList(rootDocId);
        //找出下级文件夹
        List<XtdcDirectory> xtdcDirectories = directoryMapper.selectChild(directory.getId());
        //找出下级文件
        List<XtdcAttach> xtdcAttaches = attachMapper.selectByFolderId(directory.getId());
        dirVO.setDirectory(directory);
        dirVO.setDirectoryList(xtdcDirectories);
        dirVO.setAttachList(xtdcAttaches);
        return dirVO;
    }

    @Override
    public DirVO selectChildList(XtdcDirectory xtdcDirectory) {
        DirVO dirVO = new DirVO();
        XtdcDirectory directory = directoryMapper.selectByPrimaryKey(xtdcDirectory.getId());
        List<XtdcDirectory> directories = directoryMapper.selectChild(directory.getId());
        List<XtdcAttach> attaches = attachMapper.selectByFolderId(directory.getId());
        dirVO.setDirectory(directory);
        dirVO.setDirectoryList(directories);
        dirVO.setAttachList(attaches);
        return dirVO;
    }

    @Override
    public Integer deleteDir(String teamId, String dirId) {
        XtzyUser user = ShiroUtils.getUser();
        String userCode = user.getUserCode();
        XtdcDirectory directory = directoryMapper.selectByPrimaryKey(dirId);
        //查找出当前登陆人在团队中的角色
        JltaTeamUser userQuery = new JltaTeamUser();
        userQuery.setTeamId(teamId);
        userQuery.setUserCode(userCode);
        JltaTeamUser jltaTeamUser = teamUserMapper.selectOne(userQuery);
        if ("0".equals(jltaTeamUser.getRole())) {
            return 0;
        }
        directoryMapper.deleteDir(dirId);
        attachMapper.deleteByFolder(dirId);
        return 1;
    }
}
